//
//  DeviceTool.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/9/20.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "DeviceTool.h"
#import "ZZDeviceList.h"
#define kDeviceListModelKey @"deviceListModelKey"
#define deviceListPath @"deviceList.plist"

@implementation DeviceTool

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    
    static DeviceTool *listGroupModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        listGroupModel = [super allocWithZone:zone];
        
    });
    
    return listGroupModel;
}

- (void)addSeaction:(NSString *)name {
    
    ZZDeviceList *list = [ZZDeviceList new];
    list.listName = name;
    list.deviceList = [NSMutableArray array];
    
    [self.deviceGroup.listDevice addObject:list];
    
    ZZDeviceList *defaultList = nil;
    for(ZZDeviceList *list in  self.deviceGroup.listDevice) {
        
        if ([list.listName isEqualToString:@"未分组设备"]) {
            
            defaultList =list;
        }
    }
    
    [self.deviceGroup.listDevice removeObject:defaultList];
    [self.deviceGroup.listDevice addObject:defaultList];
    
    // 保存归档
    [self save];

}

- (void)findDevice:(NSString *)name {
    
    ZZDeviceList *list = [ZZDeviceList new];
    list.listName = name;
    list.deviceList = [NSMutableArray array];
    
    [self.deviceGroup.listDevice addObject:list];
    
    ZZDeviceList *defaultList = nil;
    for(ZZDeviceList *list in  self.deviceGroup.listDevice) {
        
        if ([list.listName isEqualToString:@"发现新设备"]) {
            
            defaultList =list;
        }
    }
    
    [self.deviceGroup.listDevice removeObject:defaultList];
    [self.deviceGroup.listDevice addObject:defaultList];
}

- (void)removeDevice:(ZZDeviceModel *)model musicList:(ZZDeviceList *)musicList {
    
    NSInteger index = [self.deviceGroup.listDevice indexOfObject:musicList];
    ZZDeviceList *list =[self.deviceGroup.listDevice objectAtIndex:index];
    
    [list.deviceList removeObject:model];
    [self save];
}

- (void)moveModel:(ZZDeviceModel *)model fromList:(ZZDeviceList *)listMusic toList:(ZZDeviceList *)listModel {
    
    NSInteger fromIndex  = [self.deviceGroup.listDevice indexOfObject:listMusic];
    [self.deviceGroup.listDevice[fromIndex].deviceList  removeObject:model];
    NSInteger toIndex  = [self.deviceGroup.listDevice indexOfObject:listModel];
    
    [self.deviceGroup.listDevice[toIndex].deviceList addObject:model];
    
    [self save];
}

- (void)save {
    
    //准备路径:
    NSString *path = NSHomeDirectory();
    NSLog(@"%@",path);
    
    path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",deviceListPath]];
    //1:准备存储数据的对象
    NSMutableData *data = [NSMutableData data];
    //2:创建归档对象
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //3:开始归档
    [archiver encodeObject:self.deviceGroup forKey:kDeviceListModelKey];
    //4:完成归档
    [archiver finishEncoding];
    //5:写入文件当中
    BOOL result = [data writeToFile:path atomically:YES];
    if (result) {
        
        // 写入成功
    }
}

#pragma mark - set  & get

- (ZZDeviceGroup *)deviceGroup {
    
    if (_deviceGroup == nil) {
        
        // 从数据中拿
        NSString *path = NSHomeDirectory();
        path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",deviceListPath]];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        //创建反归档对象
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:myData];
        //反归档
        ZZDeviceGroup* listGroupModel = [unarchiver decodeObjectForKey:kDeviceListModelKey];
        //完成反归档
        [unarchiver finishDecoding];
        
        if(listGroupModel == nil) { // 如果没有则数据初始化
            
            listGroupModel = [[ZZDeviceGroup alloc] init];

            ZZDeviceList *list = [ZZDeviceList new];
            list.listName = @"默认分组";
            list.deviceList = [NSMutableArray array];

            
            
            
            
            ZZDeviceList *defalutList = [ZZDeviceList new];
            defalutList.listName = @"未分组设备";
            defalutList.deviceList = [NSMutableArray array];
            
            ZZDeviceList *findeList = [ZZDeviceList new];
            findeList.listName = @"发现新设备";
            findeList.deviceList = [NSMutableArray array];
            findeList.isFindNewDevice = YES;
            [listGroupModel.listDevice addObject:list];
            [listGroupModel.listDevice addObject:defalutList];
            // 归档
            //准备路径:
            NSString *path = NSHomeDirectory();
            NSLog(@"%@",path);
            
            path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",kCFURLPathKey]];
            //1:准备存储数据的对象
            NSMutableData *data = [NSMutableData data];
            //2:创建归档对象
            NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
            //3:开始归档
            [archiver encodeObject:listGroupModel forKey:deviceListPath];
            //4:完成归档
            [archiver finishEncoding];
            //5:写入文件当中
            BOOL result = [data writeToFile:path atomically:YES];
            if (result) {
                
                // 写入成功
            }
        }
        _deviceGroup = listGroupModel;
        
    }
    
    return _deviceGroup;
}


- (void)isExitUUID:(NSString *)uuid deviceName:(NSString *)deviceName {


    BOOL isExit = NO;
    for (ZZDeviceList *list in [DeviceTool new].deviceGroup.listDevice) {

        for (ZZDeviceModel *model in list.deviceList) {

            if ([uuid isEqualToString:model.uuid]) {

                // 存在
                isExit = YES;
#pragma 存在UUID
                model.isConnect = YES;

            } else {

                model.isConnect = NO;
            }

        }
    }

    // 如果不存在则需要建立模型，重新添加
    if(!isExit) {

        ZZDeviceModel *model = [ZZDeviceModel new];
        model.deviceName = deviceName;
        model.uuid = uuid;
        model.isConnect = YES;

        [[[DeviceTool new].deviceGroup.listDevice lastObject].deviceList addObject:model];
          [[DeviceTool new] save];

    }



    // 发出通知刷新界面
    [[NSNotificationCenter defaultCenter] postNotificationName:kFindNewDeviceConnect object:nil];
}

- (void)NOConnectDevice {

    for (ZZDeviceList *list in [DeviceTool new].deviceGroup.listDevice) {

        for (ZZDeviceModel *model in list.deviceList) {

            model.isConnect = NO;
        }
    }
}

@end
